package io.zhengsh;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class JueJinList {

    /**
     * 文章首页
     */
    private static String URL = "https://juejin.cn/user/%s/posts";

    static  {
        String userid = System.getProperty("juein.userid");
        URL = String.format(URL, userid);
    }

    /**
     * 抓包
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        Map<String, List<String>> map = readHtml2UrlMap();

        printMD(map);


    }

    public static Map<String, List<String>> readHtml2UrlMap() throws IOException {
        File postsPage = new File("/Users/zhengsh/Downloads/3306.html");
        Document doc = Jsoup.parse(postsPage, "UTF-8", URL);
        Elements entryLists = doc.getElementsByAttributeValue("name", "entry-list");

        Map<String, List<String>> map = titleAndMap();

        for (Element ele : entryLists) {
            Elements titles = ele.select(".title-row");
            for (Element row : titles) {
                String text = row.text();
                String href = row.select("a").get(0).attributes().get("href");
                //todo 需要修改
                //String li = String.format("- [%s](%s)", text, href);
                String li = href;
                //log(li);

                String key = map.keySet().stream().filter(item -> {
                    String[] s = item.split(" ");
                    String t = s[0];
                    return text.toUpperCase().contains(t.toUpperCase());
                }).findFirst().orElse("其他设计");
                map.get(key).add(li);
            }
        }
        return map;
    }

    private static void printMD(Map<String, List<String>> map) {
        map.forEach((k, li) -> {
            log(String.format("# %s", k));
            li.forEach(JueJinList::log);
        });
    }

    /**
     * 分类集合
     *
     * @return
     */
    private static Map<String, List<String>> titleAndMap() {
        Map<String, List<String>> map = new HashMap<>();
        map.put("Redis 源码设计", new ArrayList<>());
        map.put("MySQL 深入解析", new ArrayList<>());
        map.put("JVM 深入解析", new ArrayList<>());
        map.put("Spring 深入解析", new ArrayList<>());
        map.put("NIO 网络编程", new ArrayList<>());
        map.put("ElasticSearch 搜索引擎", new ArrayList<>());
        map.put("Kafka 消息队列", new ArrayList<>());
        map.put("MyBatis ORM框架", new ArrayList<>());
        map.put("Docker 服务容器", new ArrayList<>());
        map.put("LeetCode 算法", new ArrayList<>());
        map.put("Nacos 注册中心", new ArrayList<>());
        map.put("Sentinel 流控组件", new ArrayList<>());
        map.put("Zookeeper 注册中心", new ArrayList<>());
        map.put("其他设计", new ArrayList<>());
        return map;
    }

    /**
     * 输出
     *
     * @param title
     */
    private static void log(String title) {
        System.out.println(title);
    }
}
